#include "stdafx.h"

#include "OpScaleEntity.h"

#include "OpTransFormEntity.h"

#include <vector>

#include "HDiMineModel.h"

#include "tw_md.h"

#include "HDiMineView.h"

#include "dmmath.h"

#include "operatorbase.h"

#include "SelectOperator.h"

#include "resource.h"
#include "dmMatrix4x4.h"


COpScaleEntity::COpScaleEntity(HDiMineView* view,IDMAddIn *addin,int mode)
:COpTransformEntity(view,addin,mode)
{

	COpTransformEntity::m_Value=1.0f; //default value
}

COpScaleEntity::~COpScaleEntity()
{

}	

 void COpScaleEntity:: OpeatorPrompt()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	m_cmd_state=CM_Move;


    CString  strPrompt;
	strPrompt.LoadString(ID_STR_SCALE_PROMPT);

	sprintf(m_oLprompt, (LPCSTR)strPrompt,COpTransformEntity::m_Value);


	OL_PromptMessage(m_oLprompt);

}

 void  COpScaleEntity::StatusPrompt()
 {
	 AFX_MANAGE_STATE(AfxGetStaticModuleState());
	 char status[MAX_PATH];
	 CString  strPrompt;
	 strPrompt.LoadString(ID_STR_CUR_SCALE);

	 sprintf(status, (LPCSTR)strPrompt,COpTransformEntity::m_Transform_Value);
	 UpdateStatusInfo(status);

 }

 BOOL COpScaleEntity::CheckAvailable()
 {
	 return CalculateBounds(m_Bounds,m_EntOldArray);	
 }



BOOL COpScaleEntity::CalculateBounds(double & bBound,CdmEntVector& enOldArray)
{

	double bound[6],maxbound[6];
	if(enOldArray.empty()) return FALSE;

	enOldArray[0]->GetBounds(maxbound);

	for(unsigned int i=1;i<enOldArray.size();i++)
	{
		enOldArray[i]->GetBounds(bound);

		if(bound[0]<maxbound[0]) maxbound[0]=bound[0];

		if(bound[1]>maxbound[1]) maxbound[1]=bound[1];

		if(bound[2]<maxbound[2]) maxbound[2]=bound[2];

		if(bound[3]>maxbound[3]) maxbound[3]=bound[3];
	}	

	bBound=(maxbound[1]-maxbound[0])+(maxbound[3]-maxbound[2]);
	bBound/=2;

	if(bBound<FUZZ_DIST) return FALSE;

	return TRUE;
}


BOOL COpScaleEntity::CalculateScale(double &scale, dmDPoint& ptBase, dmDPoint& ptOrient)
{
	double length=0.0f;

	if(ptBase.Equal(ptOrient,FUZZ_DIST))    return FALSE;

	length=twModelFunc::ComputerLenghFromPoints(ptBase,ptOrient);

	scale=length/m_Bounds;

	return TRUE;
	
}


BOOL COpScaleEntity::CalculateTransform(dmMatrix4x4* pdmTransform, dmDPoint* pPtOrient)
{
	m_Transform_Value=COpScaleEntity::m_Value; 

	if(pPtOrient!=NULL)	
	{
		if(!CalculateScale(m_Transform_Value,m_ptBase,*pPtOrient)) return FALSE;
	}

	m_pdmMatrix->Identity();		

	m_pdmMatrix->Translate(m_ptBase.x,m_ptBase.y,m_ptBase.z);		

	m_pdmMatrix->Scale(m_Transform_Value,m_Transform_Value,m_Transform_Value);

	//m_pdmMatrix->Rotate(scale,(double*)&m_ptAxes);	

	m_pdmMatrix->Translate(-m_ptBase.x,-m_ptBase.y,-m_ptBase.z);		

	return TRUE;

}
void  COpScaleEntity::ConvertTranform()
{
	m_pdmMatrix->Identity();		

	m_pdmMatrix->Translate(m_ptBase.x,m_ptBase.y,m_ptBase.z);		

	m_pdmMatrix->Scale(1/m_Transform_Value,1/m_Transform_Value,1/m_Transform_Value);



	m_pdmMatrix->Translate(-m_ptBase.x,-m_ptBase.y,-m_ptBase.z);		

}



